#include <linux/sched.h>
#include <linux/tty.h>

struct tty_struct tty_table[] = {
    {
        {ICRNL,         // 将输入的CR转换为NL
        OPOST|ONLCR,    // 将输出的NL转换为CRNL
        0,              // 控制模式初始化为0
        ISIG | ICANON | ECHO | ECHOCTL | ECHOKE,    // 本地模式标志
        0,
        INIT_C_CC},     // 控制字符数组
        0,
        0,
        con_write,
        {0, 0, 0, 0, ""},
        {0, 0, 0, 0, ""},
        {0, 0, 0, 0, ""}
    }
};

struct tty_queue *table_list[] = {
    &tty_table[0].read_q, &tty_table[0].write_q,
};

void tty_init(void)
{
    con_init();
}

int tty_write(unsigned channel, char *buf, int nr)
{
    struct tty_struct *tty;
    char c, *b = buf;

    if (channel > 0 || nr < 0)
        return -1;
    tty = channel + tty_table;

    while (nr > 0) {
        if (current->signal)
            break;
        while (nr > 0 && !FULL(tty->write_q)) {
            c = *b;
            b++; nr--;
            PUTCH(c, tty->write_q);
        }
        tty->write(tty);
        if (nr > 0)
            schedule();
    }
    return (b - buf);
}
